package com.nearby.demand.ui.auto;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.nearby.demand.R;
import com.nearby.demand.db.Demand;
import com.nearby.demand.utils.KLog;
import com.nearby.demand.utils.WindowUtil;
import com.request.ok.http.OkHttpUtils;
import com.request.ok.http.callback.StringCallback;
import com.squareup.okhttp.Request;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Example Fragment class that shows an identifier inside a TextView.
 */
public class ColourFragment extends Fragment {

    private int identifier;
    private int colour;
    private int Values;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        colour = args.getInt("colour");
        identifier = args.getInt("identifier");
        Values = args.getInt("Values");
    }

    public class MyStringCallback extends StringCallback {
        @Override
        public void onBefore(Request request) {
            super.onBefore(request);
            KLog.i("TAG", "onBefore");
        }

        @Override
        public void onAfter() {
            super.onAfter();
            KLog.i("TAG", "onAfter");
        }

        @Override
        public void onError(Request request, Exception e) {
            KLog.i("TAG", "onError-" + e.toString());
        }

        @Override
        public void onResponse(String response) {
            KLog.i("TAG", "onResponse-" + response);
            //has protected access in
            try {
                JSONArray jsonArray = new JSONArray(response);
                int size = jsonArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject json = jsonArray.optJSONObject(i);
                    Demand demand = new Demand();
                    demand.setStBg(baseUrl + json.optString("releaseInfoUrl"));
                    demand.setStLabel(json.optString("releaseAddress"));
                    demands.add(demand);
                }
                //                Gson gson = new Gson();
                //                java.lang.reflect.Type type =
                //                        new com.google.gson.reflect.TypeToken<List<Demand>>() {
                //                        }.getType();
                //                demands = gson.fromJson(response, type);

                if (Values > 0) {
                    DraweeController controller0 = Fresco.newDraweeControllerBuilder()
                            .setUri(demands.get(0).getStBg())
                            .build();
                    DraweeController controller1 = Fresco.newDraweeControllerBuilder()
                            .setUri(demands.get(1).getStBg())
                            .build();
                    DraweeController controller2 = Fresco.newDraweeControllerBuilder()
                            .setUri(demands.get(2).getStBg())
                            .build();
                    DraweeController controller3 = Fresco.newDraweeControllerBuilder()
                            .setUri(demands.get(3).getStBg())
                            .build();
                    iv_a_left.setController(controller0);
                    iv_b_right.setController(controller1);
                    iv_c_left.setController(controller2);
                    iv_d_right.setController(controller3);
                } else {
                    DraweeController controller0 = Fresco.newDraweeControllerBuilder()
                            .setUri(demands.get(new Random().nextInt(demands.size())).getStBg())
                            .build();
                    sdview.setController(controller0);
                    int d = new Random().nextInt(demands.size());
                    textView.setText(demands.get(d).getStLabel() + "\n" + d);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        @Override
        public void inProgress(float progress) {
            KLog.i("TAG", "inProgress");
        }
    }

    List<Demand> demands = new ArrayList<Demand>();
    String baseUrl = "http://192.168.1.104:8080/MccServer";

    View view;
    SimpleDraweeView iv_a_left;
    SimpleDraweeView iv_b_right;
    SimpleDraweeView iv_c_left;
    SimpleDraweeView iv_d_right;
    SimpleDraweeView sdview;
    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (Values > 0) {
            view = inflater.inflate(R.layout.detail_grid_item, null);
            int width = WindowUtil.getScreenWidth();
            iv_a_left = (SimpleDraweeView) view.findViewById(R.id.iv_a_left);

            LinearLayout.LayoutParams a = new LinearLayout.LayoutParams(width / 2 - 20, LinearLayout.LayoutParams.MATCH_PARENT);
            a.setMargins(5, 5, 5, 5);
            LinearLayout.LayoutParams b = new LinearLayout.LayoutParams(width / 2 - 20, LinearLayout.LayoutParams.MATCH_PARENT);
            b.setMargins(5, 5, 5, 5);
            LinearLayout.LayoutParams c = new LinearLayout.LayoutParams(width / 2 - 20, LinearLayout.LayoutParams.MATCH_PARENT);
            c.setMargins(5, 5, 5, 5);
            LinearLayout.LayoutParams d = new LinearLayout.LayoutParams(width / 2 - 20, LinearLayout.LayoutParams.MATCH_PARENT);
            d.setMargins(5, 5, 5, 5);
            iv_a_left.setLayoutParams(a);
            iv_b_right = (SimpleDraweeView) view.findViewById(R.id.iv_b_right);
            iv_b_right.setLayoutParams(b);
            iv_c_left = (SimpleDraweeView) view.findViewById(R.id.iv_c_left);
            iv_c_left.setLayoutParams(c);
            iv_d_right = (SimpleDraweeView) view.findViewById(R.id.iv_d_right);
            iv_d_right.setLayoutParams(d);
        } else {
            view = inflater.inflate(R.layout.detail_list_item, null);
            sdview = (SimpleDraweeView) view.findViewById(R.id.sdview);
            textView = (TextView) view.findViewById(R.id.textView);
        }


        //        view = inflater.inflate(R.layout.detail_list_item, null);
        //        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        //        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        //        view.measure(w, h);
        //        int height = view.getMeasuredHeight();
        //        int widthS = view.getMeasuredWidth();
        //        KLog.i("TAG", "measure width=" + widthS + " height=" + height);


        OkHttpUtils
                .get()
                .url(baseUrl + "/requestIndex")
                .build()
                .execute(new MyStringCallback());

        //        TextView v = new TextView(getActivity());
        //        v.setGravity(Gravity.CENTER);
        //        v.setTextSize(40);
        //        v.setTextColor(Color.BLACK);
        //        v.setBackgroundColor(colour);
        //        v.setText("Fragment ID: " + identifier);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("dummy", true);
    }
}
